package controller

import (
	"github.com/cn-ygf/go-admin/internal/action"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
	"github.com/cn-ygf/go-admin/pkg/utils"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// ConfigController 配置管理
type ConfigController struct {
	Ctx iris.Context
}

// PostList 查询配置列表
func (c *ConfigController) PostList(req vo.QueryConfigListReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:config:list") {
		return nil
	}
	if req.PageSize > 1000 {
		req.PageSize = 1000
	}
	if req.Params != nil {
		req.Params.BeginTime = req.Params.BeginTime / 1000
		req.Params.EndTime = req.Params.EndTime / 1000
	}
	list, total, err := service.ConfigService.QueryList(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(map[string]any{
		"rows":  list,
		"total": total,
	})
}

// GetBy 根据id获取配置信息
func (c *ConfigController) GetBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:config:query") {
		return nil
	}
	cfg, err := service.ConfigService.GetByID(id)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(cfg)
}

// Put 修改配置
func (c *ConfigController) Put(req vo.EditConfigReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:config:edit") {
		return nil
	}
	if !service.ConfigService.CheckConfigKeyUnique(req.ConfigId, req.ConfigKey) {
		return action.Error("修改配置'" + req.ConfigName + "'失败，配置类型已存在")
	}
	req.UpdateBy = getCurrentUser(c.Ctx).UserName
	err := service.ConfigService.Update(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// Post 添加配置
func (c *ConfigController) Post(req vo.AddConfigReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:config:add") {
		return nil
	}
	if !service.ConfigService.CheckConfigKeyUnique(0, req.ConfigKey) {
		return action.Error("添加配置'" + req.ConfigName + "'失败，配置类型已存在")
	}
	req.CreateBy = getCurrentUser(c.Ctx).UserName
	err := service.ConfigService.Add(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// DeleteBy 删除配置
func (c *ConfigController) DeleteBy(idStr string) mvc.Result {
	if !hasPermi(c.Ctx, "system:config:remove") {
		return nil
	}
	ids := utils.IdsStrToInt64(idStr)
	if len(ids) < 1 {
		return action.ParamError()
	}

	err := service.ConfigService.Delete(ids)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// DeleteRefreshCache 清除配置缓存
func (c *ConfigController) DeleteRefreshCache() mvc.Result {
	if !hasPermi(c.Ctx, "system:config:remove") {
		return nil
	}
	err := service.ConfigService.Load()
	if err != nil {
		return action.Error(err.Error())
	}
	return action.OK()
}

// PostExport 导出配置
func (c *ConfigController) PostExport() mvc.Result {
	if !hasPermi(c.Ctx, "system:config:export") {
		return action.RawError()
	}
	configList, err := service.ConfigService.All()
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}
	titleRow := []string{"参数主键", "参数名称", "参数键名", "参数键值", "系统内置"}

	yesOrNoMaps, err := service.DictDataService.GetDictMaps("sys_yes_no")
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}
	data := make([][]any, len(configList))
	for index, item := range configList {
		row := make([]any, 6)
		row[0] = item.ConfigID
		row[1] = item.ConfigName
		row[2] = item.ConfigKey
		row[3] = item.ConfigValue
		row[4] = yesOrNoMaps[item.ConfigType]
		data[index] = row
	}
	return action.ExportExcel(data, titleRow)
}
